package com.hnit.applet.common.controller;

import cn.hutool.core.date.DateUtil;
import com.hnit.applet.common.constant.CommonConstant;
import com.hnit.applet.common.enums.ResponseStatus;
import com.hnit.applet.common.model.Meta;
import com.hnit.applet.common.model.Page;
import com.hnit.applet.common.model.Response;
import com.hnit.applet.common.model.ResponsePage;
import com.hnit.applet.common.utils.MessageSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyEditorSupport;
import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * @author zwj
 */
@Slf4j
public class BaseController {

    @Resource
    protected MessageSourceUtil messageSourceUtil;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        // Date 类型转换
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                setValue(DateUtil.parse(text));
            }
        });
    }

    protected <T> Response<T> getResponse(T data, String messageCode, Object[] args) {
        return getResponse(data, ResponseStatus.OK, messageCode, args);
    }

    protected <T> Response<T> getResponse(T data, ResponseStatus responseStatus, String messageCode, Object[] args) {
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        String apiVersion = request.getHeader(CommonConstant.ApiVersion.API_VERSION);
        //构造返回前台响应信息
        Response<T> response = new Response<>();
        response.setMeta(new Meta().toBuilder().apiVersion(apiVersion).code(String.valueOf(responseStatus.value())).
                message(messageSourceUtil.getMessage(messageCode, args)).build());
        response.setData(data);
        return response;
    }

    protected <T> ResponsePage<List<T>> getResponsePage(Page page, List<T> dataLs, Object[] args) {
        return getResponsePage(page, dataLs, ResponseStatus.OK, CommonConstant.Message.COMMON_VALID_L_QUERY, args);
    }

    protected <T> ResponsePage<List<T>> getResponsePage(Page page, List<T> dataLs, ResponseStatus responseStatus, String messageCode, Object[] args) {
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        String apiVersion = request.getHeader(CommonConstant.ApiVersion.API_VERSION);
        //构造返回前台响应信息
        ResponsePage<List<T>> responsePage = new ResponsePage<>();
        responsePage.setMeta(new Meta().toBuilder().apiVersion(apiVersion).code(String.valueOf(responseStatus.value())).
                message(messageSourceUtil.getMessage(messageCode, args)).build());
        responsePage.setPage(page);
        responsePage.setData(dataLs);
        return responsePage;
    }
}
